
MainVersion:=1
SubVersion:=00

DateVersion:=$(shell date +%y%m.%d.%H%M)

ObjectName:=

DependObjcet:=$(shell find src -maxdepth 1  -type d | grep -v .svn | sed s\#src/\#\#g | grep -v src )

DiffFileList:=$(shell svn -r $(B):$(A) diff | grep +++ | awk '{print $$2}' | awk -F/ '{print $$2}' | uniq )
DiffEtcDirNumber:=$(shell svn -r $(B):$(A) diff | grep +++ | grep src/etc | awk '{print $$2}' | wc | awk '{print $$1}' )
DiffDevDirNumber:=$(shell svn -r $(B):$(A) diff | grep +++ | grep src/dev | awk '{print $$2}' | wc | awk '{print $$1}' )
DiffGrubDirNumber:=$(shell svn -r $(B):$(A) diff | grep +++ | grep src/etc | awk '{print $$2}' | wc | awk '{print $$1}' )

PatchKernelCmd:=

DelFileForPatch:=clamav.cvd.tgz

DependFiles:=
TmpInstallDir:=TmpInstall
libv:=2.3.6
InstallDir:=$(shell pwd)/tmp/usr/local

MessageInfoStart:=Sorry. I will start to compile IMG now :-(
MessageInfoEnd:=Congratulation. I have compiled IMG and We can go on work :-)

hda:ImgName:=hda

hdc:ImgName:=hdc

.PHONY: all hda test hdc clean dist_clean root patch sys_patch message_start message_end ram_ready rec_ready

img:message_start hdc message_end 
all:message_start hdc sys_patch message_end 

patch:sys_patch

testpatch:
	echo $(DiffFileList)

$(DependObjcet):
	$(MAKE) -C src/$@

hda:$(DependObjcet) root 
hdc:$(DependObjcet) root 

message_start:
	echo "$(MessageInfoStart)" > message
	date >> time.txt
	wall message
	@rm -rf message

message_end:
	echo "$(MessageInfoEnd)" > message
	date >> time.txt
	wall message
	@rm -rf message

sys_patch:hdc
	if [ ! -d patch ]; then mkdir patch; else rm -rf patch/* ;fi
	for i in $(DiffFileList); \
	do \
	echo " ==== patch src/$$i ==== " ;\
	echo " ==== patch src/$$i ==== ">> patch.log ;\
	find src/$$i -name *.tgz -exec cp {} patch -rf \; ;\
	done
	# patch the etc dir.
	if [ $(DiffEtcDirNumber) -gt 0 ]; then cd src && tar czf ../patch/etc.tgz etc; fi
	# patch the dev dir.
	if [ $(DiffDevDirNumber) -gt 0 ]; then cd src && tar czf ../patch/dev.tgz dev; fi
	# patch the grub dir.
	if [ $(DiffGrubDirNumber) -gt 0 ]; then cp src/grub/grub.conf patch -rf; fi
	# patch the kernle
	$(PatchKernelCmd)
	#cp -rf _install/packages/* patch/
	cd patch ; rm -rf $(DelFileForPatch)
	echo "$(MainVersion).$(SubVersion).$(DateVersion)" > _install/etc/issue
	$(MAKE) -C src/upgrade clean
	$(MAKE) -C src/upgrade patch
	cp -rf src/upgrade/Upgrade.patch ./Upgrade.$(MainVersion).$(SubVersion).$(DateVersion).patch

fsy_ready:
	$(MAKE) -C dom

install_dir:
	rm -rf src/upgrade/patch
	if [ -d _install ]; then rm -fr _install; fi
	cp -fra src/busybox/_install ./
	# make some necessarily system dir 
	cd _install; mkdir -p packages proc usr lib var etc/ssh .ssh ldisk
	cd _install; if [ -e ../src/dev/dev.tar.gz ]; then tar xzf ../src/dev/dev.tar.gz; \
	else echo "ERROR: "; echo " ==== Please make dev files. ==== "; echo " "; fi

tgz_ready:
	rm -rf _install/packages/* 
	sync && sync
	cp -rf `find src -name *.tgz` _install/packages
	#cd _install/; for i in packages/*.tgz; do tar xzvf $$i; done

etc_ready:
	cp -fr src/etc _install/ && rm -rf _install/etc/Makefile
	chmod +x _install/etc/init.d/rcS
	find _install/etc -name .svn -exec rmdir {} \;
	#cp -rf src/etc/.license _install/etc
	#ln -sf /ldisk/config _install/etc/config
	#ln -sf /ldisk/config/default.xml _install/etc/config/default.xml
	#cp -rf src/httpd/encode_html/config.xml  _install/etc/config/
	#cp -rf src/httpd/encode_html/config.xml  _install/etc/config/default.xml
	echo "$(MainVersion).$(SubVersion).$(DateVersion)" > _install/etc/issue

bsh_ready:
	cp -rf /bin/bash _install/bin/
	cp -rf /sbin/ldconfig _install/sbin/ 		# BusyBox have not ldconfig command actually :(
	cp -rf /usr/bin/ldd _install/sbin/		# BusyBox have not ldd command actually :(
	cp -rf /usr/bin/strace _install/sbin/		# For debug :) and it is not necessarily !
	cp -rf /bin/mount _install/sbin/
	ln /bin/bash _install/bin/sh -sf

lib_ready:
	mkdir _install/lib/tls -p
	cp /lib/ld-$(libv).so _install/lib -f		# The init for busybox
	cp /lib/libc-$(libv).so _install/lib -f 	# The init for busybox
	cp /lib/libdl-$(libv).so _install/lib -f	# The init for iptables
	cp /lib/libcrypt-$(libv).so _install/lib -f	# The init for busybox
	cp /lib/libncurses.so.5.5 _install/lib -f	# The init for bash
	cp /lib/tls/libc-$(libv).so _install/lib/tls -f # The init for system
	cp /lib/libm-2.3.6.so _install/lib -f		# The init for busybox
	cp /lib/tls/libnsl-2.3.6.so _install/lib -f	# The init for iptables
	cp /lib/tls/libpthread-2.3.6.so _install/lib -f # The lib for apache2's httpd
	cp /lib/tls/librt-2.3.6.so _install/lib -f 	# The lib for apache2's httpd
	cp /lib/libuuid.so.1 /lib/libuuid.so.1.2 /lib/libdevmapper.so.1.02 \
	   /lib/libblkid.so.1 /lib/libblkid.so.1.0 \
	   _install/lib -af				# The lib for mount command
	cd _install/lib;\
	ln ld-$(libv).so ld-linux.so.2 -s; ln libc-$(libv).so libc.so.6 -s; ln libncurses.so.5.5 libncurses.so.5 -s;\
	ln libcrypt-$(libv).so libcrypt.so.1 -s; ln -s libdl-$(libv).so libdl.so.2; ln libnsl-2.3.6.so libnsl.so.1 -s;\
	ln libm-2.3.6.so libm.so.6 -s;ln libpthread-2.3.6.so libpthread.so.0 -s;ln librt-2.3.6.so librt.so.1 -s;

ns_switch:
	cp -dpR /lib/libnss_* _install/lib

lnk_ready:
	ln /usr/tmp  _install/tmp -sf
	ln /usr/run  _install/var/run -sf
	ln /usr/tmp  _install/var/tmp -sf
	ln /usr/log  _install/var/log -sf
	ln /usr/lock _install/var/lock -sf
	ln /usr/spool _install/var/spool -sf
	ln /ldisk/config _install/etc/config -sf
	ln /ldisk/system/.license _install/etc/.license -sf
	ln /usr/lib/modules _install/lib/modules -sf

deb_ready:
	if [ -e test.sh ]; then cp test.sh _install/ -fr ; chmod +x _install/test.sh ;fi
	if [ -e id_dsa.pub ]; then cat id_dsa.pub >_install/.ssh/authorized_keys ; fi

ram_ready:
	if [ -d $(TmpInstallDir) ]; then umount -l $(TmpInstallDir); echo "Don't execute umount command." ;\
	else mkdir -p $(TmpInstallDir) ; fi
	cp -rf dom/initrd.ramdisk ./ramdisk
	mount ramdisk $(TmpInstallDir) -o loop
	cp -ra _install/* $(TmpInstallDir) 
	cp -ra _install/.ssh $(TmpInstallDir) 
	rm -rf $(TmpInstallDir)/lost+found 
	sync && sync
	umount $(TmpInstallDir)
	rm -fr ramdisk.gz ;gzip -9 ramdisk

rec_ready:
	if [ -d $(TmpInstallDir) ]; then umount -l $(TmpInstallDir); echo "Don't execute umount command." ;\
	else mkdir -p $(TmpInstallDir) ; fi
	cp -rf dom/initrd.ramdisk ./ramdisk_rec
	mount ramdisk_rec $(TmpInstallDir) -o loop
	cp -fra src/busybox/_install/* $(TmpInstallDir)
	cd $(TmpInstallDir); mkdir -p proc usr lib var ldisk mnt
	cd $(TmpInstallDir); if [ -e ../src/dev/dev.tar.gz ]; then tar xzf ../src/dev/dev.tar.gz; \
	else echo "ERROR: "; echo " ==== Please make dev files. ==== "; echo " "; fi
	cp -fra src/recover/etc recover.sh $(TmpInstallDir) && chmod +x $(TmpInstallDir)/etc/init.d/rcS 
	cp -fra _install/tmp _install/var _install/lib $(TmpInstallDir) 
	cp -rf /bin/mount /sbin/mkfs.ext2 /sbin/ldconfig $(TmpInstallDir)/sbin
	cp -rfa /lib/libext2fs.so.* /lib/libcom_err.so.* /lib/libe2p.so.* /lib/libselinux.so.* \
	/lib/libsepol.so.* $(TmpInstallDir)/lib/
	rm -rf $(TmpInstallDir)/lost+found 
	sync && sync
	umount $(TmpInstallDir)
	rm -fr ramdisk_rec.gz ;gzip -9 ramdisk_rec

root: fsy_ready install_dir tgz_ready etc_ready bsh_ready lib_ready ns_switch lnk_ready deb_ready ram_ready rec_ready
	# Start ready env
	if [ -d $(TmpInstallDir) ]; then umount -l $(TmpInstallDir); rm  -rf $(TmpInstallDir)/* ; \
	else mkdir -p $(TmpInstallDir) ; fi
	cp -rf dom/disk.two.partition content.img
	# Start general operate for stand mini system
	mount content.img $(TmpInstallDir) -o loop
	cp -ra ramdisk.gz $(TmpInstallDir) 		
	cp -rf src/kernel/bzImage $(TmpInstallDir)/bzImage
	cp -rf src/grub/grub.conf $(TmpInstallDir)/boot/grub/
	mkdir -p $(TmpInstallDir)/upgrade $(TmpInstallDir)/system $(TmpInstallDir)/config
	echo "" > $(TmpInstallDir)/system/.license
	cp -rf src/web/encode_html/config.xml  $(TmpInstallDir)/config/config.xml
	cp -rf src/web/encode_html/config.xml  $(TmpInstallDir)/config/default.xml
	find $(TmpInstallDir) -name .svn -exec rmdir {} \;
	sync && sync
	umount $(TmpInstallDir)
	# End for stand mini system and start for revonvert system
	cp dom/disk.three.partition extend.img
	if [ -d $(TmpInstallDir) ]; then umount -l $(TmpInstallDir); rm  -rf $(TmpInstallDir)/* ; \
	else mkdir -p $(TmpInstallDir) ; fi
	mount extend.img $(TmpInstallDir) -o loop
	cp -ra ramdisk.gz $(TmpInstallDir) 		
	cp -rf src/kernel/bzImage $(TmpInstallDir)/bzImage
	cp -rf src/grub/grub.conf $(TmpInstallDir)/boot/grub/
	mkdir -p $(TmpInstallDir)/upgrade $(TmpInstallDir)/system $(TmpInstallDir)/config
	echo "" > $(TmpInstallDir)/system/.license
	cp -rf src/web/encode_html/config.xml  $(TmpInstallDir)/config/config.xml
	cp -rf src/web/encode_html/config.xml  $(TmpInstallDir)/config/default.xml
	find $(TmpInstallDir) -name .svn -exec rmdir {} \;
	sync && sync
	umount $(TmpInstallDir)
	# End for reconver system
	# End for revonvert system and start for base system
	cp dom/disk.one.partition boot.img
	if [ -d $(TmpInstallDir) ]; then umount -l $(TmpInstallDir); rm  -rf $(TmpInstallDir)/* ; \
	else mkdir -p $(TmpInstallDir) ; fi
	mount boot.img $(TmpInstallDir) -o loop
	cp -ra ramdisk_rec.gz $(TmpInstallDir)/ramdisk.gz
	cp -rf src/kernel/bzImage $(TmpInstallDir)/bzImage
	find $(TmpInstallDir) -name .svn -exec rmdir {} \;
	sync && sync
	umount $(TmpInstallDir)
	# End for base system
	cp -rf dom/boot.256.three.partition Mini-tmp.img
	cat boot.img >>  Mini-tmp.img
	cat content.img >>  Mini-tmp.img
	#+++++++++++++++++++++++++++++
	cat extend.img >> Mini-tmp.img # Final Release Is Open !!!
	#cat dom/disk.three.partition >> Mini-tmp.img
	#+++++++++++++++++++++++++++++
	mv Mini-tmp.img Mini-$(MainVersion).$(SubVersion).$(DateVersion).$(ImgName).img
	#
	# Start clean env
	rm -rf boot.img content.img extend.img $(TmpInstallDir) 

test:img
	scp ramdisk.gz 192.168.1.200:/ldisk
	ssh 192.168.1.200 sync
	ssh 192.168.1.200 reboot

clean:
	rm -rf *.tgz tmp roottmp _install $(TmpInstallDir) Upgrade.patch patch patch.log 
	for i in $(DependObjcet);do $(MAKE) clean -C src/$$i;done
	$(MAKE) clean -C dom

dist_clean:clean
	rm -rf $(ObjectDir) $(ObjcetSource)
	for i in $(DependObjcet);do $(MAKE) dist_clean -C src/$$i;done
	$(MAKE) dist_clean -C dom
	rm -rf *.img  *.patch *.gz
